//<!-- License -->
package eu.datafusion.log4soa.model.referencedata;

import static javax.persistence.CascadeType.ALL;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import eu.datafusion.log4soa.model.DataProtection;
import javax.persistence.JoinColumn;

/**
 * Service is container for operations 
 * @author Ivo.Mitov@datafusion.eu
 *
 */
@Entity
@Table(name = "SERVICES")
@Cacheable
public class Service extends BaseReferenceData implements Serializable {

	/**
	 * generated
	 */
	private static final long serialVersionUID = 2739260225475239978L;

	/**
	 * application required immutable property
	 */
	@ManyToOne(optional = false)
	@JoinColumn(name = "APP_ID", referencedColumnName = "ID", updatable = false, nullable = false)
	private Application application;
	
	/**
	 * operations
	 */
	@OneToMany(mappedBy = "service", orphanRemoval = true, cascade = ALL)
	private Set<Operation> operations;
	
	/**
	 * @param application required immutable property <tt>application</tt>
	 * @param name required immutable property <tt>name</tt> must be unique per Service class instances
	 * @param dataProtection
	 * @param description optional property
	 */
	public Service(Application application,String name,DataProtection dataProtection, String description) {
		super(name,dataProtection,description);
		this.setApplication(application);
	}
	
	/**
	 * @param application required immutable property <tt>application</tt>
	 * @param name required immutable property <tt>name</tt> must be unique per Service class instances
	 * @param dataProtection
	 */
	public Service(Application application,String name,DataProtection dataProtection) {
		this(application,name,dataProtection,null);
	}
	
	/**
	 * @param application required immutable property <tt>application</tt>
	 * @param name required immutable property <tt>name</tt> must be unique per Service class instances
	 * @param description optional property
	 */
	public Service(Application application,String name,String description) {
		this(application,name,DataProtection.UNPROTECTED,description);
	}
	
	/**
	 * @param application required immutable property <tt>application</tt>
	 * @param name required property <tt>name</tt> must be unique per Service class instances
	 */
	public Service(Application application,String name) {
		this(application,name,DataProtection.UNPROTECTED);
	}
	
	/**
	 * default constructor required by JPA
	 */
	protected Service(){
		
	}

	/**
	 * Getter of the required immutable property <tt>application</tt>
	 * @return  Returns the application.
	 */
	public Application getApplication() {
		return application;
	}
	
	/**
	 * Setter of the required immutable property <tt>application</tt>
	 * @return  Returns the application.
	 */
	private void setApplication(Application application) {
		if(application==null){
			throw new IllegalArgumentException("Application can't be null");
		}
		this.application=application;
	}

	/**
	 * Getter of the property <tt>operations</tt> init if null
	 * @return  Returns the operations.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
	 */
	public Set<Operation> getOperations() {
		if(operations==null){
			operations = new HashSet<Operation>();
		}
		return operations;
	}

	/**
	 * add Operation to Service
	 * @param operation  The operation to add.
	 */
	public void addOperations(Operation operation) {
		this.getOperations().add(operation);
	}

	@Override
	public String toString() {
		return super.toString() + ", application=" + application + ", operations=" + operations;
	}
	
	

}
